package com.wmlive.hhvideo.heihei.discovery.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wmlive.hhvideo.common.GlobalParams;
import com.wmlive.hhvideo.common.base.DcBaseFragment;
import com.wmlive.hhvideo.heihei.beans.discovery.LocalVideoBean;
import com.wmlive.hhvideo.heihei.beans.subject.TopicInfo;
import com.wmlive.hhvideo.heihei.discovery.DiscoveryUtil;
import com.wmlive.hhvideo.heihei.discovery.adapter.SearchVideoAdapter;
import com.wmlive.hhvideo.heihei.recordvideo.DyUIAPIManager;
import com.wmlive.hhvideo.utils.CollectionUtil;
import com.wmlive.hhvideo.utils.KLog;
import com.wmlive.hhvideo.utils.PermissionUtils;
import com.wmlive.hhvideo.utils.SdkUtils;

import java.util.List;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by lsq on 6/13/2017.
 * 选择本地视频的页面
 */

public class SearchVideoFragment extends DcBaseFragment implements SearchVideoAdapter.OnVideoSelectListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tvBlank)
    TextView tvBlank;
    private SearchVideoAdapter searchVideoAdapter;
    private Disposable disposable;

    public static SearchVideoFragment newInstance(String startType, TopicInfo topicInfo) {
        SearchVideoFragment fragment = new SearchVideoFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(LAZY_MODE, true);
        bundle.putString("startType", startType);
        bundle.putParcelable("topicInfo", topicInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_search_video;
    }

    @Override
    protected void onSingleClick(View v) {

    }

    @Override
    protected void initData() {
        super.initData();
        setTitle("本地视频", true);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        searchVideoAdapter = new SearchVideoAdapter();
        searchVideoAdapter.setOnVideoSelectListener(this);
        recyclerView.setAdapter(searchVideoAdapter);

        if (!(SdkUtils.isMarshmallow() && !PermissionUtils.hasSDCardRWPermission(getContext()))) {
            final long start = System.currentTimeMillis();
            KLog.i("开始扫描视频：" + start);
            disposable = DiscoveryUtil.getAllVideos(getActivity())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<List<LocalVideoBean>>() {
                        @Override
                        public void accept(List<LocalVideoBean> localVideoBeen) throws Exception {
                            if (GlobalParams.Config.IS_DEBUG) {
                                for (LocalVideoBean bean : localVideoBeen) {
                                    KLog.i("扫描到视频：" + bean.toString());
                                }
                            }
                            if (CollectionUtil.isEmpty(localVideoBeen)) {
                                findViewById(R.id.llBlankPanel).setVisibility(View.VISIBLE);
                                findViewById(R.id.ivBlank).setVisibility(View.VISIBLE);
                                tvBlank.setText(R.string.search_local_video_null);
                                recyclerView.setVisibility(View.GONE);
                            } else {
                                findViewById(R.id.llBlankPanel).setVisibility(View.GONE);
                                recyclerView.setVisibility(View.VISIBLE);
                                searchVideoAdapter.addData(localVideoBeen);
                            }
                            KLog.i("完成扫描视频：" + (System.currentTimeMillis() - start));
                        }
                    });
        }
    }

    @Override
    public void onVideoSelect(LocalVideoBean bean) {
        if (bean != null && !TextUtils.isEmpty(bean.getPath())) {
            DyUIAPIManager.getInstance().onOpenLocalVideoInfos(getActivity(), bean.getPath());
            getActivity().finish();
        } else {
            showToast(R.string.hintErrorDataDelayTry);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != disposable && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }

}
